package com.grouponmall.web;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.grouponmall.model.Deal;

/**
 * @author yijiang
 * @since 2010-6-30
 * @version 1.0
 * 
 */
@Controller
public class AdminController extends BaseController {

  private static final Integer DEFAULT_PAGE_SIZE = 20;

  @RequestMapping("/admin/rA.htm")
  public String rA() throws Exception {
    retrieveFromAdilityDeal.retrieve();
    return "admin/result";
  }

  @RequestMapping("/admin/rG.htm")
  public String rG() throws Exception {
    retrieveFromGroupon.retrieve();
    return "admin/result";
  }

  @RequestMapping("/admin/rH.htm")
  public String rH() throws Exception {
    retrieveFromHomeRun.retrieve();
    return "admin/result";
  }

  @RequestMapping("/admin/rS.htm")
  public String rS() throws Exception {
    retrieveFromSocialBuy.retrieve();
    return "admin/result";
  }

  @RequestMapping("/admin/rT.htm")
  public String rT() throws Exception {
    retrieveFromTippr.retrieve();
    return "admin/result";
  }

  @RequestMapping("/admin/manageDeals.htm")
  public String manageDeals(Model model, @RequestParam(required = false) Integer index) {
    index = index == null ? 1 : index;
    List<Deal> deals = dealService.getDealsByPagination(index, DEFAULT_PAGE_SIZE);
    model.addAttribute("deals", deals);
    return "admin/manageDeals";
  }

  @RequestMapping("/admin/updateDeal.htm")
  public String updateDeal(Model model, @RequestParam(value = "deal", required = true) String dealString) {
    String[] params = dealString.split("-,-");
    long id = Long.valueOf(params[0]);
    Deal deal = dealService.getDealById(id);
    if (deal != null) {
      deal.setCity(params[1]);
      deal.setSource(params[2]);
      deal.setTitle(params[3]);
      deal.setPrice(params[4]);
      deal.setRetailValue(params[5]);
      deal.setDiscount(params[6]);
      deal.setTimeEnd(params[7]);
      if (params[8].equals("true")) {
        deal.setOnlineDeal(true);
      } else {
        deal.setOnlineDeal(false);
      }
    }
    dealService.updateDeal(deal);
    return "admin/result";
  }

}
